package com.rhb.mail.mail.web.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.*;
import java.text.*;
import org.apache.struts.action.*;

import com.rhb.mail.mail.bean.Mail;
import com.rhb.mail.web.struts.action.MailBaseDispatchAction;
import com.rhb.mail.mail.web.struts.form.MailForm;
import com.rhb.ca.uzer.bean.Uzer;
import com.rhb.ca.web.util.HttpConnect;
import com.rhb.mail.mail.bean.*;


public class MailCtrlAction extends MailBaseDispatchAction{
	public ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		//System.out.println("unspecified for defalut parameter!");
		return sentList(mapping,form,request,response);
	}

	
	public ActionForward deletedReceivedList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;

		oform.getMailQuery().setReceivedPersonPK("rhb");
		oform.getMailQuery().setMadePersonPK("");
		oform.getMailQuery().setMadePersonName("");
		
		int allcount = this.getMailBusiness().getAllCount_findDeletedReceivedMailByQuery(oform.getMailQuery());
		List list = null;
		if ( allcount != 0) {
			list = this.getMailBusiness().findDeletedReceivedMailByQuery(oform.getMailQuery(),oform.getStart(),oform.getCount());
		}else{
			list = (List)new ArrayList();
		}

		oform.setAllCount(allcount);
		oform.setList(list);
		return mapping.findForward("deletedReceivedList");
	}
	
	public ActionForward deletedSentList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;

		oform.getMailQuery().setMadePersonPK("rhb");
		oform.getMailQuery().setReceivedPersonPK("");
		
		int allcount = this.getMailBusiness().getAllCount_findDeletedSentMailByQuery(oform.getMailQuery());
		List list = null;
		if ( allcount != 0) {
			list = this.getMailBusiness().findDeletedSentMailByQuery(oform.getMailQuery(),oform.getStart(),oform.getCount());
		}else{
			list = (List)new ArrayList();
		}

		oform.setAllCount(allcount);
		oform.setList(list);
		return mapping.findForward("deletedSentList");
	}

	
	public ActionForward unreadList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;

		oform.getMailQuery().setReceivedPersonPK("rhb");
		oform.getMailQuery().setMadePersonPK("");
		oform.getMailQuery().setMadePersonName("");
		
		int allcount = this.getMailBusiness().getAllCount_findUnreadMailByQuery(oform.getMailQuery());
		List list = null;
		if ( allcount != 0) {
			list = this.getMailBusiness().findUnreadMailByQuery(oform.getMailQuery(),oform.getStart(),oform.getCount());
		}else{
			list = (List)new ArrayList();
		}

		oform.setAllCount(allcount);
		oform.setList(list);
		return mapping.findForward("unreadList");
	}
	
	public ActionForward sentList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;

		oform.getMailQuery().setMadePersonPK("rhb");
		oform.getMailQuery().setReceivedPersonPK("");
		
		int allcount = this.getMailBusiness().getAllCount_findSentMailByQuery(oform.getMailQuery());
		List list = null;
		if ( allcount != 0) {
			list = this.getMailBusiness().findSentMailByQuery(oform.getMailQuery(),oform.getStart(),oform.getCount());
		}else{
			list = (List)new ArrayList();
		}

		oform.setAllCount(allcount);
		oform.setList(list);
		return mapping.findForward("sentList");
	}

	public ActionForward receivedList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;

		oform.getMailQuery().setReceivedPersonPK("rhb");
		oform.getMailQuery().setMadePersonPK("");
		oform.getMailQuery().setMadePersonName("");
		
		int allcount = this.getMailBusiness().getAllCount_findReceivedMailByQuery(oform.getMailQuery());
		List list = null;
		if ( allcount != 0) {
			list = this.getMailBusiness().findReceivedMailByQuery(oform.getMailQuery(),oform.getStart(),oform.getCount());
		}else{
			list = (List)new ArrayList();
		}

		oform.setAllCount(allcount);
		oform.setList(list);
		return mapping.findForward("receivedList");
	}

	public ActionForward show(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;
			
		List list = this.getMailBusiness().findMailsByFatherPk(oform.getMail().getFatherPk());
		if(list == null){
			list = (List)new ArrayList();
		}
		oform.setList(list);
		return mapping.findForward("show");
	}
	
	
	public ActionForward managerList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;

		int allcount = this.getMailBusiness().getAllCount_findByQuery(oform.getMailQuery());
		List list = null;
		if ( allcount != 0) {
			list = this.getMailBusiness().findByQuery(oform.getMailQuery(),oform.getStart(),oform.getCount());
		}else{
			list = (List)new ArrayList();
		}

		oform.setAllCount(allcount);
		oform.setList(list);
		return mapping.findForward("managerList");
	}
	
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;
		oform.setMail(new Mail());

		oform.getMail().setMadeDate(new Date());
		Uzer uzer = HttpConnect.getUzer(request);
		if(uzer!=null){
			oform.getMail().setMadePersonPK(uzer.getUzerPk());
			oform.getMail().setMadePersonName(uzer.getUzerName());

		}
		
		saveToken(request);  //
		return mapping.findForward("add");
	}

	public ActionForward resend(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;
	
		String fatherPk = oform.getMail().getFatherPk();
		oform.setMail(new Mail());
		oform.getMail().setFatherPk(fatherPk);

		oform.getMail().setMadeDate(new Date());
		Uzer uzer = HttpConnect.getUzer(request);
		if(uzer!=null){
			oform.getMail().setMadePersonPK(uzer.getUzerPk());
			oform.getMail().setMadePersonName(uzer.getUzerName());

		}
		
		saveToken(request);  //
		return mapping.findForward("add");
	}

	public ActionForward reply(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;
	
		String fatherPk = oform.getMail().getFatherPk();
		oform.setMail(new Mail());
		oform.getMail().setFatherPk(fatherPk);

		oform.getMail().setMadeDate(new Date());
		Uzer uzer = HttpConnect.getUzer(request);
		if(uzer!=null){
			oform.getMail().setMadePersonPK(uzer.getUzerPk());
			oform.getMail().setMadePersonName(uzer.getUzerName());
		}
		
		saveToken(request);  //
		return mapping.findForward("reply");
	}

	
	
	public ActionForward edit(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;
		oform.setMail(this.getMailBusiness().findByPk(oform.getMail().getMailPk()));

		saveToken(request);
		return mapping.findForward("edit");
	}
	
	public ActionForward view(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;
		oform.setMail(this.getMailBusiness().findByPk(oform.getMail().getMailPk()));
		return mapping.findForward("view");
	}
	
	public ActionForward managerView(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		MailForm oform = (MailForm)form;
		oform.setMail(this.getMailBusiness().findByPk(oform.getMail().getMailPk()));
		return mapping.findForward("managerView");
	}
	
	public ActionForward erase(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		return mapping.findForward("erase");
	}
	
	public ActionForward del(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response){
		//System.out.println("********* mailCtrl.del() *******");
		
		return mapping.findForward("del");
	}
}
